from ninja import Router
from rest_framework import serializers

from apps.commom.good import GoodSimpleSerializer
from apps.schemas import R
from models.models import BrowsingRecord
from utils.auth import AppletAuthBearer
from utils.decorators import handle_exception

browsing_router = Router(tags=["商品浏览记录"], auth=AppletAuthBearer())


class BrowsingRecordSerializer(serializers.ModelSerializer):
    good = serializers.SerializerMethodField()

    def get_good(self, obj):
        return GoodSimpleSerializer(obj.good).data

    class Meta:
        model = BrowsingRecord
        fields = "__all__"


@browsing_router.get("/browsing_records", summary="浏览记录")
@handle_exception("获取浏览记录失败")
def get_browsing_records(request):
    records = BrowsingRecord.objects.filter(user_id=request.user.id)
    return R.ok(BrowsingRecordSerializer(records, many=True).data)
